package yby;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("u");
        String password = req.getParameter("p");
        String t = req.getParameter("t");

        String sql = "SELECT `id` FROM `usertable` WHERE `username`=? AND `password`=md5(?)";
        //Integer id = JdbcUtils.select(sql, Integer.class, username, password);

//        Integer id = JdbcUtils.select(sql, Integer.class, username, password);
        Integer id = JdbcUtils.select(sql, Integer.class, username, password);

        HttpSession session = req.getSession();
        Object tom = session.getAttribute("yby");

//        boolean isTrue = t != null && (t.equals(tom) || t.equals("tom"));
//        if (!isTrue) {
//            resp.sendRedirect("/Tomxk/login.jsp");
//            return;
//        }

        if (id!=null && id>0) {
            session.setAttribute("UID",id);
            resp.sendRedirect("/yby1/index.jsp");
        } else {
            resp.sendRedirect("/yby1/login.jsp");
        }


       //String message = id != null && id > 0 ? "登陆成功":"登录失败";




//        System.out.println("username="+username);
//        System.out.println("passowrd="+password);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}